#ifndef SENDICMP_H
#define SENDICMP_H


#include <QString>
#include <QThread>
#include"winsock2.h"
#include "ws2tcpip.h"
#include "Format.h"


const int ICMP_REQUEST = 8;
const int ICMP_RESEPONSE = 0;
const int ICMP_TTLE = 11;

const int ICMP_MAX_HOP = 30;
const int ICMP_MAX_TIMEOUT = 3000;
const QString TRACE_EXAMPLE = "baidu.com";
const int ICMP_DATA_SIZE = 32;
const char PADDING_LETTER = 'A';
const int ICMP_MAX_LEN = 1024;


class sendIcmp:public QThread
{
    Q_OBJECT;
private:
    int max_hop;
    int timeout;
    QString ip_name;
    SOCKET raw_sock;
    ICMP_HEADER *icmp;
    sockaddr_in des_sock_addr;
    sockaddr_in recv_sock_addr;
    u_long ip;
    char padding_letter;
    bool reach_des;
    int ttl;
    bool is_over;
    u_short sequence;
    u_long time_stamp;
    char sender_buffer[sizeof(ICMP_HEADER) + ICMP_DATA_SIZE];
    char recv_buffer[ICMP_MAX_LEN];

signals:
    void send(QString);

protected:
    void run() override;
public:
    sendIcmp();
    u_short generateChecksum(u_short *p_buffer, int size);
    bool Init();
    bool assembleData();

    bool sendData(u_short seq);
    bool RecvData();

    bool CheckRecvData(char *recv_buffer, int size);

    void setIpName(QString ip_name);
    void setMaxHop(int max_hop);
    void setTimeOut(int timeout);
    void setKill();
    void setPadding(QString letter);
};

#endif // SENDICMP_H
